﻿using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Repositories.Account;
using Gfw.Repositories.Manage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gfw.Infrastructure.Facilities
{
    /// <summary>
    /// 统计公司利润，只统计前一天的，且不可重复统计
    /// </summary>
    public class ProfitStatisticsSettlement : IFacility
    {
        private DateTime _end;

        #region 构造函数

        /// <summary>
        /// 
        /// </summary>
        /// <param name="end">前一天</param>
        public ProfitStatisticsSettlement(DateTime end)
        {
            this._end = end;
        }

        #endregion

        #region IFacility 成员

        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            if (unitOfWork != null)
                throw new GfwTransactionInvalidException("统计公司利润，必须以独立事务运行。");

            DateTime yesterdayStart = this._end.AddSeconds(1); // 00:00:01
            DateTime yesterdayEnd = this._end.AddDays(1).AddSeconds(-1); // 23:59:59

            ActCompanyStatisticsprofitRepository repository = new ActCompanyStatisticsprofitRepository();
            var list = repository.Query(DateTime.Today.AddSeconds(1), DateTime.Today.AddDays(1).AddSeconds(-1)); // 判断有没有当天的记录，如果有，表示已经进行过统计！

            decimal sumAnnualFee, sumSellStock, sumToCashTax, sumTax, sumChangeInformation, 
                sumBonus, sumOthers;
            if (list == null ||
                (list != null && list.Count == 0)) // 还未对前一天进行过任何统计操作
            {
                ActCompanyBargainRepository comBargainRep = new ActCompanyBargainRepository();
                if(comBargainRep.StatisticsCompanyProfit(yesterdayStart, yesterdayEnd,
                    out sumAnnualFee, out sumSellStock, out sumToCashTax, out sumTax, out sumChangeInformation, 
                    out sumBonus, out sumOthers)) // 操作成功
                {
                    // 写入统计表
                    StringBuilder payAbstract = new StringBuilder("统计时间：");
                    payAbstract.AppendLine(this._end.ToShortDateString());

                    //////////////////////////////////////////////////////////////////////////
                    // 这里可以做成hyperlink，可以查询详细
                    //////////////////////////////////////////////////////////////////////////
                    if (sumBonus + sumOthers != 0)
                    {
                        payAbstract.Append("支出共计：");
                        payAbstract.AppendLine((sumBonus + sumOthers).ToString("C"));
                    }

                    repository.AddProfit(DateTime.Now, sumAnnualFee, sumSellStock, sumToCashTax,
                        sumTax, sumChangeInformation, sumBonus, sumOthers, payAbstract.ToString());
                }
            }
            else
            {
                throw new GfwVerifyException("只能对前一天未进行过统计操作！");
            }
        }

        #endregion
    }
}
